<?php
/**
 * ClassName: bed
 * Description: 床位管理控制器
 * @Author 周昌
 * @Create 2023/11/8 13:33
 */

namespace app;


use epii\server\Args;
use model\BedModel;
use model\RoomModel;
use service\admin\HospitalService;
use service\admin\BedService;
use service\admin\RoomService;

class bed extends base
{
    public function init()
    {
        parent::init();
        $this->accessDenied();
    }
    public function index()
    {
        $room_id=Args::params('room_id/d');
        if($this->adminInfo['role']==1){
            $hospitals=HospitalService::getSelect(['status'=>1]);
            $this->assign('hospital_select',$hospitals);
        }
        $this->_as_room_id=$room_id;
        //筛选
        $this->_as_is_empty = BedService::bedIsEmptySelect();
        $this->_as_room_select = RoomService::getSelect(['hospital_id'=>$this->adminInfo['hospital_id']]);
        $this->adminUiDisplay();
    }

    public function ajax_data()
    {
        $map = [];
        //登录用户权限判断
        $hospital_id=$this->adminInfo['hospital_id'];
        if($hospital_id){
            $map[]=['hospital_id','=',$hospital_id];
        }
        $room_id=Args::params('room_id/d');
        if($room_id){
            $map[]=['room_id','=',$room_id];
        }
        //空床筛选
        $is_empty=Args::params('is_empty/d');
        if(is_int($is_empty)){
            $map[]=['is_empty','=',$is_empty];
        }
        $name = Args::params('name');
        if ($name) {
            $map[] = ['name', "LIKE", '%' . $name . '%'];
        }
        echo BedService::getAjaxData($map);
    }

    public function add_edit()
    {
        $id = Args::params('id');
        $room_id = Args::params('room_id');
        if ($_SERVER['REQUEST_METHOD'] == "POST") {
            $data = Args::params('r');
            if($room_id){
                $data['room_id']=$room_id;
            }
            //验证
            $this->validate($data,[
                'name|床位名称/编号'=>'require',
                'room_id|所属病房'=>'require|integer|>=:0',
                'status|状态'=>'require|integer',
            ]);
            if ($id) {
                $res = BedService::edit($id,$data);
            } else {
                $data['addtime']=time();
                $data['hospital_id']=RoomModel::getValue(['id'=>$data['room_id']],'hospital_id');
                $res = BedService::add($data);
            }
            $res['code']?$this->success($res['msg']):$this->error($res['msg']);
        }
        $this->_as_info = $id ? BedModel::getInfo(['id'=>$id]) : [];
        $this->_as_id = $id;
        $this->_as_room_id = $room_id;
        $this->_as_status = BedService::statusSelect();
        $this->_as_is_empty = BedService::bedIsEmptySelect();
        $this->_as_rooms = RoomService::getSelect(['hospital_id'=>$this->adminInfo['hospital_id']]);

        $this->adminUiDisplay();
    }
    //修改状态
    public function status(){
        $id=Args::params('id/d/1','id,不能为空');
        $status=Args::params('status');
        $res=BedService::status($id,$status);
        if($res['code']){
            $this->success('成功','refresj');
        }
        $this->error();
    }

    /**
     * @return array|false|string
     * @throws \think\Exception
     * @throws \think\db\exception\PDOException
     * 删除
     */
    public function del()
    {
        $id = Args::params('id');
        //todo 判断该事项下是否已经有床位，无则可以删除
        $res = BedService::del($id);
        if ($res['code']) {
            $this->success($res['msg'],'refresh');
        } else {
            $this->error($res['msg']);
        }
    }

    public function getRoomByHospital()
    {
        $hospital_id=Args::params('hospital_id/d/1','');
        $list=RoomService::getSelect(['hospital_id'=>$hospital_id]);
        exit(json_encode($list));
    }
    //根据room_id 获取房间下空bed
    public function getBedByRoom()
    {
        $room_id=Args::params('room_id/d/1','');
        $list=BedService::getSelect(['room_id'=>$room_id,'is_empty'=>BedService::$is_empty_index['yes']]);
        exit(json_encode($list));
    }
    //设置空床/非空床
    public function setEmpty()
    {
        $id=Args::params('id/d');
        $is_empty=Args::params('is_empty');
        $res=BedService::setEmpty($id,$is_empty);
        if($res['code']){
            $this->success('成功','refresh');
        }
        $this->error();
    }
}